package animationEngine;

import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;

/**
 * The Class StringBoxDisplay, this class represents the string view display which displays
 * the string and pattern at a set size, which may then only mean showing part of the text
 * that is to be searched.
 * 
 * Methods in this class allow us to move the text that is being searched to show certain
 * ranges of the text and also deal with highlighting characters etc.
 * 
 * @author Andrei Palade
 * @author David McBrierty
 * @author Garry Sharp
 * @author Hristo Georgiev
 * @author John Dennis
 * @author Marco Sarconi
 * @version 1.0.1
 */
public class StringBoxDisplay extends JInternalFrame {

	/** Generated serial version ID. */
	private static final long serialVersionUID = -7308549895315390916L;
	
	//The string box to hold the string that is being searched
	/** The text box. */
	public StringBox textBox;
	//The panel to hold the textBox StringBox
	/** The text box panel. */
	public JPanel textBoxPanel;
	//The StringBox to hold the pattern
	/** The pattern box. */
	public StringBox patternBox;
	//The panel to hold the patternBox StringBox
	/** The pattern box panel. */
	public JPanel patternBoxPanel;
	//Swap button to swap panel views
	/** The swap button. */
	public JButton swapButton;
	//Panel to hold the button
	/** The swap button panel. */
	public JPanel swapButtonPanel;
	
	/** The new height. */
	public double newHeight;
	
	/** The new width. */
	public double newWidth;
	
	/**
	 * Instantiates a new string box display.
	 *
	 * @param text the text
	 * @param pattern the pattern
	 */
	public StringBoxDisplay(String text, String pattern) {
		super("String Box Display", true, false, false, true);		
		//Set Layout to null so we can position the panels manually
		this.setLayout(null);	
		
		//Set up the text box panel and the textBox
		textBoxPanel = new JPanel();
		
		this.textBox = new StringBox(text);
				
        //Set the PANELS location
        textBoxPanel.setLocation(0, 0);
        textBoxPanel.setSize(textBox.getSize().width,textBox.getSize().height);
        //Set the TEXTBOXs location
        textBox.setLocation(0, 0);
        //Add text box to the text box panel
        textBoxPanel.add(textBox);
        //Add the text box panel to us
        this.add(textBoxPanel);
                
        //Set up the pattern box panel and the PatternBox
        patternBoxPanel = new JPanel();
        /*this.patternBox = new StringBox("a p");*/
        this.patternBox = new StringBox(pattern);
        //Set the PANELS location
        patternBoxPanel.setLocation(0, 40);
        patternBoxPanel.setSize(patternBox.getSize().width,patternBox.getSize().height);
        //Set the PATTERNBOXs location
        patternBox.setLocation(0,40);
        //Add pattern box to its panel
        patternBoxPanel.add(patternBox);
        //Add the pattern box panel to us
        this.add(patternBoxPanel);
        //patternBoxPanel.setVisible(false);
        //textBoxPanel.setVisible(false);
        
        
        //Set to opaque - perhaps not required
        ///this.setOpaque(true);		
		//Set out preffered size so that it can hold the pattern and text box
		this.setPreferredSize(new Dimension(this.textBox.getSize().width+10, 3*this.textBox.getSize().height));
		
		//Set our location in our parent container
		this.setLocation(150, 150);
		this.pack();
		this.setVisible(true);		
		
		//method for adding the swap button.
        //placeSwapButton(this.getSize());
	}
	
	/**
	 * Sets the pattern and text.
	 *
	 * @param pattern the pattern
	 * @param text the text
	 */
	public void setPatternAndText(String pattern, String text) {
		//patternBoxPanel.remove(patternBox);
		this.updateUI();
		patternBoxPanel.removeAll();
		patternBox = new StringBox(pattern);
		//textBoxPanel.remove(textBox);
		textBoxPanel.removeAll();
		textBox = new StringBox(text);
	
		System.out.format("New pattern: %s\nNew Text: %s\n", pattern, text);
		
		textBoxPanel.add(textBox);
		patternBoxPanel.add(patternBox);
		
		//this.validate();
	}
	
	/**
	 * Gets the text.
	 *
	 * @return the text
	 */
	public String getText() {
		return textBox.getStringUsed();
	}
	
	/**
	 * Gets the pattern.
	 *
	 * @return the pattern
	 */
	public String getPattern() {
		return patternBox.getStringUsed();
	}
	
	//swap button addition bones
	/*public void placeSwapButton(Dimension intFrameDim){
		double width = intFrameDim.getWidth(); double height = intFrameDim.getHeight();
		System.out.println(3*this.textBox.getSize().height);
		swapButtonPanel = new JPanel();
		this.swapButton = new JButton("swap");
		this.swapButton.setSize(70,20);
		swapButtonPanel.setLocation((int) width - 30 - swapButton.getSize().width,
				(int) height - 30 -swapButton.getSize().height);//this.getSize().width - 20, this.getSize().height - 20);
		swapButtonPanel.setSize(swapButton.getSize().width, swapButton.getSize().height);
		swapButton.setLocation(this.textBox.getSize().width -50 - swapButton.getSize().width, 3*this.textBox.getSize().height - 10);//this.getSize().width - 20, this.getSize().height - 20);        
		swapButtonPanel.add(swapButton);       
		this.add(swapButton);
	}*/
	
	/**
	 * This method is used to shift the pattern the given distance inside us.
	 * 
	 * The actual container that moves is the panel that holds the patternBox in it.
	 * 
	 * @param distance Integer representing the distance to move
	 */
	public void shiftPattern(int distance){
		
		//If we are trying to move the pattern forward
		if (distance > 0){
			//Check that there is enough room to shift it forward (ie it wont go off the panel)
			if (patternBoxPanel.getLocation().x + distance + patternBoxPanel.getWidth() > this.getWidth()){
				//System.out.println("Shifting pattern will take it out of view");
				return;
			}
			//Move the pattern
			this.patternBoxPanel.setLocation(this.patternBoxPanel.getLocation().x + distance, this.patternBoxPanel.getLocation().y);
		}
		else {
			//Check that there is enough room to shift it forward (ie it wont go off the panel)
			if (patternBoxPanel.getLocation().x + distance < 0){
				//System.out.println("Shifting pattern will take it out of view");
				return;
			}
			//Move the pattern
			this.patternBoxPanel.setLocation(this.patternBoxPanel.getLocation().x + distance, this.patternBoxPanel.getLocation().y);
		}
	}
	
	/**
	 * Move pattern to.
	 *
	 * @param location the location
	 */
	public void movePatternTo(int location){
		
		//If we are trying to move the pattern forward
		if (location > 0){
			//Check that there is enough room to shift it forward (ie it wont go off the panel)
			if (location + patternBoxPanel.getWidth() > this.getWidth()){
				//System.out.println("Shifting pattern will take it out of view");
				return;
			}
			//Move the pattern
			this.patternBoxPanel.setLocation(location, this.patternBoxPanel.getLocation().y);
		}
		else {
			//Check that there is enough room to shift it forward (ie it wont go off the panel)
			if (location < 0){
				//System.out.println("Shifting pattern will take it out of view");
				return;
			}
			//Move the pattern
			this.patternBoxPanel.setLocation(location, this.patternBoxPanel.getLocation().y);
		}
	}
	
		
	/**
	 * This method is used to reset the pattern to the start of the currently displayed string.
	 */
	public void resetPattern(){
		this.patternBoxPanel.setLocation(0, this.patternBoxPanel.getLocation().y);
	}
	
	/**
	 * Reset text.
	 */
	public void resetText(){
		this.textBoxPanel.setLocation(0, this.textBoxPanel.getLocation().y);
	}
	
	/**
	 * This method is used to reset the current index of the String to 0, which will
	 * place the string back at the start.
	 */
	public void resetString(){
		this.textBox.resetCurrentIndex();
	}
	
	/**
	 * Clear highlighting.
	 */
	public void clearHighlighting(){
		this.textBox.clearAllHighlighting();
		this.patternBox.clearAllHighlighting();
	}
	
	/**
	 * This method is used to shift the string the given distance.
	 *
	 * @param distance the distance
	 */
	public void shiftString(int distance){
		this.textBox.incrementCurrentIndex();
		this.textBox.reconstruct();
		//this.textBoxPanel.setLocation(0, 15);
		//this.textBox.setLocation(0, 15);
	}
	
	

}
